package top.kaoshanji.p1ch03b;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 代码清单-p1ch03b-02：携带授权码重定向到客户端
 * @author kaoshanji
 * ^_^
 * create time 2025/5/25 13:52
 */
@WebServlet(urlPatterns = {"/sendAuthCodeServlet"})
public class SendAuthCodeServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 设置请求对象的编码
        req.setCharacterEncoding(StandardCharsets.UTF_8.name());

        // 用户通过表单请求处理授权设置权限范围
        String responseType = req.getParameter("response_type");
        String redirectUri =req.getParameter("redirect_uri");
        String clientId = req.getParameter("client_id");

        // 授权码请求
        if ("code".equals(responseType)) {
            String code = "code-" + System.nanoTime();

            String toClientUrl = redirectUri + "?code=" + code;

            resp.sendRedirect(toClientUrl);
        }

    }

}
